package com.datasecops.scannerservices.controller;

import com.datasecops.scannerservices.config.KafkaConfig;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.TopicPartition;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.XMLConstants;
import javax.xml.soap.Node;
import java.util.List;
import java.util.logging.XMLFormatter;

/**
 * 消费者控制类
 * @author: SunLingzhi
 * @time: 2022/7/29 15:03
 */
@Configuration
public class ConsumerController {

    //日志输出
    private final Logger logger = LoggerFactory.getLogger(getClass());

    //@KafkaListener(topics = topic)
    //@KafkaListener(id = "original1",groupId = "original", topics = {KafkaConfig.TOPIC_FIRST})
    public void consumerTopic(String msg){
        System.out.println("consumerTopic::收到消息：" + msg);
    }

     /**
      * 扫描命令监听
      * @Description
      * @author SunLingzhi
      * @time 2022/8/2 10:46
      * @param
      * @return
      */
 /*   @KafkaListener(id = KafkaConfig.group_instruct,groupId = KafkaConfig.group_instruct_0, topicPartitions = {
            @TopicPartition(topic = KafkaConfig.TOPIC_INSTRUCT,partitions = {KafkaConfig.PARTITIONS_START+""})
    })*/
    public void scannerJobListener(String msg){
        disposeMsg(msg);
        System.out.println("consumerTopicPartitions::收到消息：" + msg);
    }

     /**
      * 消息处理
      * @Description
      * @author SunLingzhi
      * @time 2022/8/2 11:00
      * @param
      * @return
      */
    private void disposeMsg(String msg) {
        try {
            Document document = DocumentHelper.parseText(msg);
            Element rootElement = document.getRootElement();
            Element LastScan = rootElement.element("LastScan");
            Element OcrLib = rootElement.element("OcrLib");
            Element Server = rootElement.element("Server");
            String ip = Server.attributeValue("Ip");

            System.out.println("aaa");

            logger.info("scannerJobListener :: disposeMsg info:" + ip);
        } catch (Exception e) {
            logger.warn("Failed method scannerJobListener :: disposeMsg error" + e.getMessage());
        }
    }
}
